package com.wangjiyuan.shiwupai.mine.activitiy;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.wangjiyuan.shiwupai.R;
import com.wangjiyuan.shiwupai.base.BaseActivity;

import java.util.Map;
import java.util.Set;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private Toolbar toolBar_login_back;
    private ImageView img_QQ_login, img_WEIXIN_login;
    private UMAuthListener umAuthListenerQQ, umAuthListenerWEIXIN;
    private UMShareAPI mShareAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initToolBar();
        initView();
    }

    private void initView() {
        img_QQ_login = (ImageView) findViewById(R.id.img_QQ_login);
        img_WEIXIN_login = (ImageView) findViewById(R.id.img_WEIXIN_login);

        img_QQ_login.setOnClickListener(this);
        img_WEIXIN_login.setOnClickListener(this);
    }

    private void initToolBar() {
        toolBar_login_back = (Toolbar) findViewById(R.id.toolBar_login_back);
        toolBar_login_back.setTitle("登录");
        toolBar_login_back.setNavigationIcon(R.mipmap.icon_back_black);
        toolBar_login_back.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View view) {
        mShareAPI = UMShareAPI.get(LoginActivity.this);
        switch (view.getId()) {
            case R.id.img_QQ_login:
                mShareAPI.doOauthVerify(LoginActivity.this, SHARE_MEDIA.QQ, umAuthListenerQQ);
                umAuthListenerQQ = new UMAuthListener() {
                    @Override
                    public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {

                        if (action == UMAuthListener.ACTION_AUTHORIZE) {
                            mShareAPI.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.QQ, umAuthListenerQQ);
                        } else {
                            Set<String> strings = data.keySet();
                            Log.e("TAG", "--------"+strings);
                        }
                        finish();
                    }

                    @Override
                    public void onError(SHARE_MEDIA platform, int action, Throwable t) {
                        Toast.makeText(getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA platform, int action) {
                        Toast.makeText(getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
                    }
                };
                break;
            case R.id.img_WEIXIN_login:
                mShareAPI.doOauthVerify(LoginActivity.this, SHARE_MEDIA.WEIXIN, umAuthListenerWEIXIN);
                umAuthListenerWEIXIN = new UMAuthListener() {
                    @Override
                    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                        Toast.makeText(getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                        Toast.makeText(getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media, int i) {
                        Toast.makeText(getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
                    }
                };
        }
    }
}
